package com.hxgis.statisticanalysis.intelligentIndicator.controller;

import com.hxgis.statisticanalysis.common.config.ResponseData;
import com.hxgis.statisticanalysis.common.controller.BaseController;
import com.hxgis.statisticanalysis.intelligentIndicator.dao.jpa.FailureRecordJpa;
import com.hxgis.statisticanalysis.intelligentIndicator.dao.jpa.ModernIndexJpa;
import com.hxgis.statisticanalysis.intelligentIndicator.pojo.FailureRecord;
import com.hxgis.statisticanalysis.intelligentIndicator.pojo.IndexConfig;
import com.hxgis.statisticanalysis.intelligentIndicator.pojo.ModernIndex;
import com.hxgis.statisticanalysis.intelligentIndicator.service.IntelligentIndicatorService;
import com.hxgis.statisticanalysis.intelligentIndicator.vo.CityNetWorkVO;
import com.hxgis.statisticanalysis.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.xmlbeans.XmlException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@Slf4j
@Api(tags = "广西-现代化指标监控")
@RestController
@RequestMapping("/statisticanalysis/frontapi/v1")
public class IntelligentIndicatorController extends BaseController {
    @Autowired
    private ModernIndexJpa modernIndexJpa;
    @Autowired
    private IntelligentIndicatorService intelligentIndicatorService;
    @Autowired
    private FailureRecordJpa failureRecordJpa;

    @ApiOperation("根据时间查询指标数据（yyyy）")
    @GetMapping("/getDataByTime")
    public ResponseData<Object> getDataByTime(String dateTime) {
        Map<String, Object> dataByTime = intelligentIndicatorService.getDataByTime(dateTime);
        return render(dataByTime);
    }

    @ApiOperation("保存修改数据")
    @PostMapping("/updateDataById")
    public ResponseData<Object> updateDataById(@RequestBody ModernIndex modernIndex) {
        try {
            modernIndexJpa.save(modernIndex);
            intelligentIndicatorService.calculateNetWorkScore(modernIndex.getDateTime());
        } catch (Exception e) {
            e.printStackTrace();
            return render("保存失败");
        }
        return render("保存成功");
    }

    @ApiOperation("excel导出（yyyy）")
    @GetMapping("/exportExcel")
    public ResponseData<Object> exportExcel(String dateTime, HttpServletResponse response) throws IOException {
        String[] split = dateTime.split(",");
        String s = intelligentIndicatorService.exportExcel(response, split);
        if (StringUtils.isNotEmpty(s)){
            return render(s);
        }
        return null;
    }

/*    @ApiOperation("分数计算（yyyy）")
    @GetMapping("/calculateNetWorkScore")
    public ResponseData<Object> calculateNetWorkScore(String dateTime) throws IOException {
        intelligentIndicatorService.calculateNetWorkScore(dateTime);
        return render("success");
    }*/

    @ApiOperation("按时间范围查询网络故障数据（yyyy-MM）")
    @GetMapping("/getNetWorkMalfunctionByDate")
    public ResponseData<List<FailureRecord>> getNetWorkMalfunctionByDate(String date) throws IOException {
        List<FailureRecord> byDeclareTime = failureRecordJpa.findByDateTime(date);
        Collections.sort(byDeclareTime, (o1, o2) -> (int) (DateUtil.str2DateByFormat(o1.getDeclareTime(), DateUtil.WEB_FORMATOR_YMDH3).getTime() - DateUtil.str2DateByFormat(o2.getDeclareTime(), DateUtil.WEB_FORMATOR_YMDH3).getTime()));
        return render(byDeclareTime);
    }

    @ApiOperation("获取详细的网络故障信息（yyyy-MM）")
    @GetMapping("/getNetWorkMalfunctionDetailsByDate")
    public ResponseData<List<CityNetWorkVO>> getNetWorkMalfunctionDetailsByDate(String date) throws IOException {
        List<CityNetWorkVO> netWorkInfo = intelligentIndicatorService.getNetWorkInfo(date);
        return render(netWorkInfo);
    }

    @ApiOperation("修改网络故障数据")
    @PostMapping("/updateNetWorkMalfunction")
    public ResponseData<Object> updateNetWorkMalfunction(@RequestBody List<FailureRecord> failureRecord) throws IOException {
        try {
            failureRecordJpa.saveAll(failureRecord);
        } catch (Exception e) {
            return render("保存失败");
        }
        return render("保存成功");
    }

    @ApiOperation("网络故障数据导出（yyyy-MM）")
    @GetMapping("/exportNetWorkExcel")
    public ResponseData<Object> exportNetWorkExcel(String date, HttpServletResponse response) throws IOException, XmlException, InvalidFormatException {
        String s = intelligentIndicatorService.exporNetWorktExcel(response, date);
        if (StringUtils.isNotEmpty(s)) {
            return render(s);
        }
        return null;
    }

    @ApiOperation("现代化指标配置获取（yyyy）")
    @GetMapping("/getIndexConfig")
    public ResponseData<List<IndexConfig>> getIndexConfig(String dateTime) throws IOException, XmlException, InvalidFormatException {
        List<IndexConfig> indexConfigByDateTime = intelligentIndicatorService.getIndexConfigByDateTime(dateTime);
        return render(indexConfigByDateTime);
    }

    @ApiOperation("现代化指标配置修改删除接口")
    @PostMapping("/updateIndexConfig")
    public ResponseData<Object> updateIndexConfig(@RequestBody List<IndexConfig> dataList) throws IOException, XmlException, InvalidFormatException {
        String s = intelligentIndicatorService.updateIndexConfig(dataList);
        return render(s);
    }


}
